<template>
	<view class="fullPage p30">
		<view class="container plr30 pt80 bf border-r2 shadow">
			<label class="flex_a h80 mb50 bdb">
				<text class="w160 c9 whn">身份证号</text>
				<input type="idcard" v-model="dataForm.idCard" class="flex1 pw0" placeholder="请输入" />
			</label>
			<label class="flex_a h80 mb50 bdb">
				<text class="w160 c9 whn">真实姓名</text>
				<input type="text" v-model="dataForm.trueName" class="flex1 pw0" placeholder="请输入" />
			</label>
			<label class="flex_a h80 mb200 bdb">
				<text class="w160 c9 whn">手机号</text>
				<input type="number" v-model="dataForm.phone" class="flex1 pw0" placeholder="请输入" />
			</label>
			
			<view class="plr30 mb30"><u-button type="primary" shape="circle" @click="savaInfo">保存</u-button></view>
		</view>
	</view>
</template>

<script>
import { Validator } from '@/utils/validate'
import {editUserInfo} from '@/api/user.js'
export default {
	data() {
		return {
			dataForm: {
				idCard: '',
				trueName: '',
				phone: ''
			}
		}
	},
	onLoad() {
		const userInfo = this.$store.state.user.userInfo
		this.dataForm.idCard = userInfo.id_card
		this.dataForm.trueName = userInfo.true_name
		this.dataForm.phone = userInfo.phone
	},
	methods: {
		async savaInfo() {
			const validator = new Validator()
			validator.add(this.dataForm.idCard, [
				{
					strategy: 'isNoEmpty',
					errorMsg: '请输入身份证号！'
				},
				{
					strategy: 'isIdCard',
					errorMsg: '请输入正确的身份证号！'
				},
				
			])
			validator.add(this.dataForm.trueName, [
				{
					strategy: 'isNoEmpty',
					errorMsg: '请输入真实姓名！'
				},
				
				
			])
			validator.add(this.dataForm.phone, [
				{
					strategy: 'isNoEmpty',
					errorMsg: '请输入手机号！'
				},
				{
					strategy: 'isMoblie',
					errorMsg: '请输入正确的手机号！'
				}
			])
			
			
			let errorMsg = validator.start()
			if (errorMsg) {
				return this.toast(errorMsg)
			}
			
			const res = await editUserInfo(this.dataForm)
			
			await this.$store.dispatch('getUserInfo')
			
			await this.confirm({
				content: '修改成功',
				showCancel: false
			})
			uni.navigateBack({
				delta: 1
			})
			
		}
	}
}
</script>

<style lang="scss"></style>
